package com.ciwong.epaper.modules.reciteWords.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.TimeoutError;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.util.ListenSpeakUtil;
import com.ciwong.epaper.modules.reciteWords.bean.JiaoCaiDetailBean;
import com.ciwong.epaper.modules.reciteWords.bean.LearnBookDetail;
import com.ciwong.epaper.modules.reciteWords.bean.ShowJiaoCaiInfo;
import com.ciwong.epaper.modules.reciteWords.dao.WordDao;
import com.ciwong.epaper.modules.reciteWords.ui.SelectVocabularyActivity;
import com.ciwong.epaper.modules.reciteWords.util.WordJumpManager;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.DialogUtil;
import com.ciwong.epaper.util.IntentValue;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by keepon on 2018/9/13.
 */
public class VocabularyRecyclerViewAdapter extends BaseRecyclerViewAdapter<ShowJiaoCaiInfo, VocabularyRecyclerViewAdapter.VocabularyViewHolder> {


	public VocabularyRecyclerViewAdapter(Context context, List<ShowJiaoCaiInfo> datas) {
		super(context, datas);
	}

	@Override
	public VocabularyViewHolder onCreateMyViewHolder(@NonNull ViewGroup parent, int viewType) {
		View inflate = LayoutInflater.from(mContext).inflate(R.layout.layout_card_item, parent, false);
		return new VocabularyViewHolder(inflate);
	}

	@Override
	public void onBindMyViewHolder(@NonNull VocabularyViewHolder holder, int position) {
		holder.bindData(mDatas.get(position));
	}

	public class VocabularyViewHolder extends RecyclerView.ViewHolder {
		private BookAdapter  mBookAdapter;
		private TextView     mTvTypeName;
		private RecyclerView mBookRecyclerView;
		private List<JiaoCaiDetailBean> mJiaoCaiDetailBeanList = new ArrayList<>();

		public VocabularyViewHolder(View itemView) {
			super(itemView);
			mTvTypeName = itemView.findViewById(R.id.tv_type_name);
			mBookRecyclerView = itemView.findViewById(R.id.recycler_view);
			mBookRecyclerView.setFocusable(false);
			mBookRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
			mBookAdapter = new BookAdapter(mContext, mJiaoCaiDetailBeanList);
			mBookRecyclerView.setAdapter(mBookAdapter);
			mBookAdapter.setOnItemClickListener(new OnItemClickListener<JiaoCaiDetailBean>() {
				@Override
				public void onItemClick(int positon, final JiaoCaiDetailBean jiaoCaiDetailBean) {
					try {
						final SelectVocabularyActivity selectVocabularyActivity = (SelectVocabularyActivity) mContext;
						if (jiaoCaiDetailBean.isSelected()) {
							selectVocabularyActivity.finish();
							return;
						}
						final int userId = (int) EApplication.getInstance().getUserInfoBase().getUserId();
						//未创建过计划
						if (jiaoCaiDetailBean.getStatus() == 0) {
							WordJumpManager.jumpToMakeWordPlanActivity(R.string.go_back, (Activity) mContext, jiaoCaiDetailBean, 0, false);
						} else if (jiaoCaiDetailBean.getStatus() == 2) { //计划已经完成
							String tips = mContext.getResources().getString(R.string.learn_again);
							DialogUtil.showConfirmDialog(mContext, tips, "确定", "取消", new DialogUtil.OnConfirmClickListener() {
								@Override
								public void onConfirmClick() {
									WordDao.getInstance().getAgain(userId, jiaoCaiDetailBean.getVersionId(), new BaseExtCallBack(mContext) {
										@Override
										public void failed(Object data) {
											super.failed(data);
											selectVocabularyActivity.hideCricleProgress();
											if (!NetworkUtils.isOnline()) {
												ToastUtil.INSTANCE.toastCenterNoNetError();
											} else if (data instanceof TimeoutError) {
												ToastUtil.INSTANCE.toastCenterError("连接超时，请稍后重试");
											}
										}

										@Override
										public void failed(int errorCode, Object data) {
											super.failed(errorCode, data);
											selectVocabularyActivity.hideCricleProgress();
											ToastUtil.INSTANCE.toastCenterErrorWithErrorCode(R.string.server_error, errorCode);
										}

										@Override
										public void success(Object data) {
											selectVocabularyActivity.hideCricleProgress();
											//再来一遍时，上次完成时间置为0，下次进来时，应该是学习中状态
											CWSys.setSharedLong(ListenSpeakUtil.getCompleteTimeKey(jiaoCaiDetailBean.getVersionId()), 0);
											WordJumpManager.jumpToWordPlanActivity(R.string.go_back, IntentValue.JumpWordPlanType.FROM_CHANGE_PLAN, null, (Activity) mContext);
										}
									});

								}
							});
						} else {
							Log.e("TAG", "onItemClick: jiaoCaiDetailBean.getStatus()==" + jiaoCaiDetailBean.getStatus() + " jiaoCaiDetailBean.getLearnCount()==" + jiaoCaiDetailBean.getLearnCount());
							selectVocabularyActivity.showCricleProgress();
							WordDao.getInstance().getBookLearnInfo((int) userId, jiaoCaiDetailBean.getVersionId(), new BaseExtCallBack(mContext) {
								@Override
								public void failed(Object data) {
									super.failed(data);
									selectVocabularyActivity.hideCricleProgress();
									if (!NetworkUtils.isOnline()) {
										ToastUtil.INSTANCE.toastCenterNoNetError();
									} else if (data instanceof TimeoutError) {
										ToastUtil.INSTANCE.toastCenterError("连接超时，请稍后重试");
									}
								}

								@Override
								public void failed(int errorCode, Object data) {
									super.failed(errorCode, data);
									selectVocabularyActivity.hideCricleProgress();
									ToastUtil.INSTANCE.toastCenterErrorWithErrorCode(R.string.server_error, errorCode);
								}

								@Override
								public void success(Object data) {
									selectVocabularyActivity.hideCricleProgress();
									if (data != null) {
										LearnBookDetail learnBookDetail = (LearnBookDetail) data;
										//表示重新制定计划
										if (learnBookDetail.getCompletecount() == learnBookDetail.getWordsum()) {

										} else {
											WordJumpManager.jumpToMakeWordPlanActivity(R.string.go_back, (Activity) mContext, jiaoCaiDetailBean, learnBookDetail.getCompletecount(), false);

										}

									}

								}
							});
						}
					} catch (Exception e) {
					}
				}
			});
		}

		public void bindData(ShowJiaoCaiInfo showJiaoCaiInfos) {
			mTvTypeName.setText(showJiaoCaiInfos.getPeriodName());
			mBookAdapter.updateDatas(showJiaoCaiInfos.getJiaoCaiDetailBeanList());
		}
	}
}
